package com.situ.water_read.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.situ.util.ResponseData;
import com.situ.water_read.pojo.RdChangemaxvalue;
import com.situ.water_read.pojo.RdChangevalue;
import com.situ.water_read.pojo.RdRead;
import com.situ.water_read.pojo.RdVolume;
import com.situ.water_read.service.RdChangemaxvalueService;
import com.situ.water_read.service.RdReadService;
import com.situ.water_system.pojo.SyEmp;
import com.situ.water_user.pojo.UsUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Angel
 * @since 2020-12-01
 */
@RestController
@RequestMapping("/rd-read")
public class RdReadController {
    @Autowired
    private RdReadService service;
    @Autowired
    private RdChangemaxvalueService maxservice;
    @GetMapping("/query")
    public List<RdRead> queryAllUser(){
        List<RdRead> list=service.queryall();

        return list;
    }

    /**
     * 抄表录入中的表册下拉框的辖区
     * @return
     */
    @GetMapping("/queryarea")
    public List<Map> queryarea(){

        List<Map> map=service.queryarea();
        return map;
    }
    /**
     * 抄表录入中的表册下拉框的表册
     * @return
     */
    @GetMapping("/queryvolume")
    public List<Map> queryvolume(HttpSession session){
        SyEmp user = (SyEmp) session.getAttribute("user");
        if(user==null){
            List<Map> map=service.queryvolume(1);
            return map;
        }else{
            List<Map> map=service.queryvolume(user.getId());
            return map;
        }


    }
    /**
     *
     */
    @GetMapping("/queryvolumeNameAndCountAndRead")
    public ResponseData queryvolumeNameAndCountAndRead(Integer volumeID){
        //表册名
        RdVolume volume = service.querybyVolumeID(volumeID);
        //查询该表册的总用户
        Integer count=service.selectCount(volumeID);
        //查询本月所属该抄表员表册的用户
        List<Map> list = service.queryReadbyID(volumeID);
        Map map=new HashMap();
        map.put("volume1",volume);
        map.put("count",count);
        map.put("list",list);
        return ResponseData.ok().putDataValue("map",map);
    }
    @PostMapping("/updateread")
    public List<Map> updateread(RdRead read){
      service.updatereadById(read);

        if(read.getAmount()==0){
            //零吨水位
            System.out.println("零吨水位");
        }else{
           // 添加分类水量
           RdRead rdReads=service.querybyID(read.getId());
           read.setReadEmpID(rdReads.getId());
           read.setFormula(rdReads.getFormula());
           service.insertReadTypes(read);
        }
       List<Map> list = service.queryReadbyID(read.getVolumeId());
        return list;
    }
    @PutMapping("/updateAudit")
    public List<Map> updateAudit(Integer Id,Integer volumeId){
        service.updateAudit(Id);
        List<Map> list = service.queryReadbyID(volumeId);
        return list;
    }
    @PutMapping("/updateMaxVlaue")
    public void updateMaxVlaue(RdRead read,HttpSession session){
        SyEmp user = (SyEmp) session.getAttribute("user");
        System.out.println(read.getId());
        //修改抄表中的最大值
       service.updateMax(read);
       if(user==null){
           int i=service.addChangeMax(read,1);
       }else {
           int i=service.addChangeMax(read,user.getId());
       }


    }

    /**
     * 审核查询所有表侧
     * @return
     */
    @GetMapping("/getAllDate")
    public List<Map> getAllDate(){
        List<Map> list=service.queryAllDate();
        return list;
    }
    /**
     * 审核查询表册
     */
    @GetMapping("/queryAuditByID")
    public List<Map> queryAuditByID(Integer volumeId){
        List<Map> list=service.queryAuditByID(volumeId);
        return list;
    }

    /**
     * 审核查询用户
     */
    @GetMapping("/queryAuditByUserNo")
    public ResponseData queryAuditByUserNo(String UserNo){
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("userno",UserNo);
        UsUser user=service.querybyuserNo(wrapper);
        List<Map> list=service.queryAuditByUserNo(UserNo);
        list.forEach(x-> System.out.println("用户姓名"+x.get("UserNo")));
        System.out.println(list.size());
        if(list.size()<=0){
            return ResponseData.Failure("没有"+user.getUsername()+"用户的未复核记录");
        }
       return ResponseData.ok().putDataValue("list",list);
    }
    @PostMapping("/addpy")
    public List<Map> addpy(String[] Id,Integer volumeId,HttpSession session){
        SyEmp user = (SyEmp) session.getAttribute("user");
        for (int i=0;i<Id.length;i++){
            if(user==null) {
                service.updateAduittrue(Id[i], 1);//修改抄表数据已审核
            }else{
                service.updateAduittrue(Id[i], user.getId());//修改抄表数据已审核
            }
            RdRead read = service.querybyID(Integer.parseInt(Id[i]));
            service.addPy(read);
        }
        List<Map> list=service.queryAuditByID(volumeId);
        return list;
    }

    /**
     * 报表抄表详情
     */
    @GetMapping("/queryReadAll")
    public  ResponseData queryReadAll(RdRead read,String yearmonth,@RequestParam(defaultValue = "",required = false)String date1,@RequestParam(defaultValue = "",required = false) String date2,@RequestParam(defaultValue ="1",required = false) int current ,
                                  @RequestParam(defaultValue = "2",required = false) int size) throws Exception {
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        if(date1!=null && !"".equals(date1)) {
            Date datebefore = sf.parse(date1);
            read.setDateBefore(datebefore);
        }
        if(date2!=null && !"".equals(date2)) {
            Date dateafter = sf.parse(date2);
            read.setDateAfter(dateafter);
        }
            if(yearmonth==null || "".equals(yearmonth)) {
                Calendar cal = Calendar.getInstance();
                read.setReadMonth(cal.get(Calendar.MONTH) + 1);
                read.setReadYear(cal.get(Calendar.YEAR));
            }else{
                String[] s = yearmonth.split(",");
                read.setReadYear(Integer.parseInt(s[0]));
                read.setReadMonth(Integer.parseInt(s[1]));
            }
        Page<RdRead> page=new Page<>(current,size);
        IPage<Map> page1=service.queryReadAll(page,read);
        Integer sum=service.selectSum(read);
        System.out.println(sum);
      //  List<Map> map=service.queryReadAll(read);
        Map map =new HashMap();
        map.put("page1",page1);
        map.put("sum",sum);
        System.out.println(map);
        return ResponseData.ok().putDataValue("map",map);
    }

    @GetMapping("/queryVolumeAll")
    public List<Map> queryVolumeAll(String yearmonth){
        Integer year;
        Integer month;
        if(yearmonth==null || "".equals(yearmonth)) {
            Calendar cal = Calendar.getInstance();
            month=cal.get(Calendar.MONTH) + 1;
            year=cal.get(Calendar.YEAR);
        }else{
            String[] s = yearmonth.split(",");
            year=Integer.parseInt(s[0]);
            month=Integer.parseInt(s[1]);
        }
        List<Map> map=service.queryVolumeAll(year,month);
        return map;
    }

    @GetMapping("/queryReadZero")
    public ResponseData queryReadZero(RdRead read,String yearmonth1,String yearmonth2,@RequestParam(defaultValue = "",required = false)String date1,@RequestParam(defaultValue = "",required = false) String date2,@RequestParam(defaultValue ="1",required = false) int current ,
                                      @RequestParam(defaultValue = "2",required = false) int size){
        //RdRead read = new RdRead();
        if(yearmonth1==null || "".equals(yearmonth1)) {
            Calendar cal = Calendar.getInstance();
            read.setMdate1(cal.get(Calendar.MONTH) + 1);
            read.setYdate1(cal.get(Calendar.YEAR));
        }else{
            String[] s = yearmonth1.split(",");
            read.setYdate1(Integer.parseInt(s[0]));
            read.setMdate1(Integer.parseInt(s[1]));
        }
        if(yearmonth2==null || "".equals(yearmonth2)) {
            Calendar cal = Calendar.getInstance();
            read.setMdate2(cal.get(Calendar.MONTH) + 1);
            read.setYdate2(cal.get(Calendar.YEAR));
        }else{
            String[] s = yearmonth2.split(",");
            read.setYdate2(Integer.parseInt(s[0]));
            read.setMdate2(Integer.parseInt(s[1]));
        }
        Page<RdRead> page=new Page<>(current,size);
        IPage<Map> page1 =service.queryReadZero(page,read);
        Integer sum=service.queryReadZeroSum(read);
        Map map=new HashMap();
        map.put("page1",page1);
        map.put("sum",sum);
        return ResponseData.ok().putDataValue("map",map);
    }
    @GetMapping("/queryemp")
    public List<SyEmp> queryemp(){
        List<SyEmp> list= service.queryemp();
        return list;
    }
    @GetMapping("/querymax")
    public IPage<Map> querymax(RdChangemaxvalue maxval,String datemax1,String datemax2,@RequestParam(defaultValue = "",required = false)String date1,@RequestParam(defaultValue = "",required = false) String date2,@RequestParam(defaultValue ="1",required = false) int current ,
                                           @RequestParam(defaultValue = "2",required = false) int size) throws ParseException {
        QueryWrapper wrapper=new QueryWrapper();
        if(maxval.getEmpID()!=null){
            wrapper.eq("r.EmpID",maxval.getEmpID());
        }
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        if(datemax1!=null && !"".equals(datemax1)){
            Date parse1 = sdf.parse(datemax1);
            wrapper.gt("Datee",parse1);
        }
        if(datemax2!=null && !"".equals(datemax2)){
            Date parse2 = sdf.parse(datemax2);
            wrapper.lt("Datee",parse2);
        }

        Page<RdChangemaxvalue> page=new Page<>(current,size);
        IPage<Map> iPage = maxservice.querymax(page, wrapper);
        return iPage;
    }
    @GetMapping("/metervalue")
    public IPage<Map> metervalue(String month,@RequestParam(defaultValue = "",required = false)String date1,@RequestParam(defaultValue = "",required = false) String date2,@RequestParam(defaultValue ="1",required = false) int current ,
                                 @RequestParam(defaultValue = "2",required = false) int size) throws ParseException {

        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        Date parse = null;
        if(month!=null && !"".equals(month)){
             parse = sdf.parse(month);
        }
        Page<Map> page=new Page<>(current,size);
        IPage<Map> iPage =service.metervalue(page, parse);
        return iPage;
    }

    @GetMapping("/querywater")
    public List<Map> querywater(){
        List<Map> list=service.querywater();
        return list;
    }
}

